package fyp.spedmew;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

/**
 * Created by chengys on 18/12/2014.
 */
public class ListViewArrayAdapter extends ArrayAdapter<ListViewClass> {

    public ListViewArrayAdapter(Context context, List<ListViewClass> items) {
        super(context, R.layout.itemlist, items);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;

        if(convertView == null) {
            // inflate the GridView item layout
            LayoutInflater inflater = LayoutInflater.from(getContext());
            convertView = inflater.inflate(R.layout.itemlist, parent, false);

            // initialize the view holder
            viewHolder = new ViewHolder();
            viewHolder.iIcon = (ImageView) convertView.findViewById(R.id.iIcon);
            viewHolder.iTitle = (TextView) convertView.findViewById(R.id.iTitle);
            viewHolder.iDescription = (TextView) convertView.findViewById(R.id.iDescription);
            viewHolder.iName =(TextView) convertView.findViewById(R.id.iName);
            viewHolder.iStatus =(TextView) convertView.findViewById(R.id.iStatus);
            viewHolder.iStatus_icon =(ImageView) convertView.findViewById(R.id.iStatus_icon);
            viewHolder.iPrice =(TextView) convertView.findViewById(R.id.iPrice);
            viewHolder.AlertIcon =(ImageView) convertView.findViewById(R.id.reqAlert);

            convertView.setTag(viewHolder);
        } else {
            // recycle the already inflated view
            viewHolder = (ViewHolder) convertView.getTag();
        }

        // update the item view
        ListViewClass item = getItem(position);
        viewHolder.iIcon.setImageDrawable(item.getIcon());
        viewHolder.iTitle.setText(item.getTitle());
        viewHolder.iDescription.setText(item.getDescription());
        viewHolder.iName.setText(item.getName());
        viewHolder.iStatus.setText(item.getStatus());
        viewHolder.iStatus_icon.setImageDrawable(item.getStatus_icon());
        viewHolder.iPrice.setText(item.getPrice());
        viewHolder.AlertIcon.setImageDrawable(item.getAlertIcon());

        return convertView;
    }


    private static class ViewHolder {
        ImageView iIcon;
        TextView iTitle;
        TextView iDescription;
        TextView iStatus;
        ImageView iStatus_icon;
        ImageView AlertIcon;
        TextView iName;
        TextView iPrice;
    }
}
